<?php
namespace app\backend\controller;

class Setting extends Backend {
	
	public function index() {
		
		$view = \think\View::instance();
		$request = \think\Request::instance();
		
		$where = array();
		$condition = array();
		$condition['title'] = $request->get('title', '');
		if(0 < mb_strlen($condition['title'], 'utf8')){
			$where['setting.name'] = array('like', '%%'.$condition['title'].'%%');
		};
		$view->assign('condition', $condition);
		
		$field = array(
			'`setting`.*',
		);
		$banner_select = \app\common\model\SettingModel::field($field)
			->where($where)->order('setting.sort ASC')->paginate(10, false, array('query'=>$condition));
		$view->assign('page', $banner_select->render());
		
		$data_list = array();
		foreach($banner_select as $StaffModel){
			$data = $StaffModel->toArray();
			$data_list[] = $data;
		};
		$view->assign('data_list', $data_list);
		
		unset($where, $condition, $field, $banner_select, $StaffModel);
		return $view->fetch();
	}
	
	public function edit_detail() {
		
		$request = \think\Request::instance();
		
		$id = $request->get('id');
		if(!is_string($id) || (1 !== preg_match('/^[0-9]{1,10}$/', $id)) || !settype($id, 'integer') || (0 >= $id)){
			$this->returnError('参数校验失败');
		};
		
		$SettingModel = \app\common\model\SettingModel::get($id);
		if(null === $SettingModel){
			$this->returnError('目标信息未找到');
		};
		
		if(true ===  $request->isAjax()){
			
			$parameter = $request->post();
			$judge_1 = array_key_exists('content', $parameter) && is_string($parameter['content']) && (20 >= strlen($parameter['content']));
			if(!$judge_1){
				return $this->disposeJson(1, '参数校验失败');
			};
			
			if($SettingModel->value === $parameter['content']){
				return $this->disposeJson(0, '内容无修改');
			};
			
			$time = time();
			$SettingModel->value = $parameter['content'];
			$SettingModel->update_time = $time;
			$result = $SettingModel->save();
			if(false == $result){
				return $this->disposeJson(1, '保存数据失败，请刷新后重试');
			};
			
			return $this->disposeJson(0, '保存成功');
			
		}else{
			
			$view = \think\View::instance();
			
			$view->assign('setting', $SettingModel->getData());
			
			return $view->fetch();
			
		};
		
	}
}